import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.less']
})
export class NavbarComponent implements OnInit {

  public userInfo:any = JSON.parse(window.localStorage.getItem('userInfo'));
  constructor(private http:HttpClient,private router:Router) { }

  ngOnInit() {
  }

  logOutHandler(){
    this.http.delete('http://localhost:3000/session')
      .toPromise()
      .then(res=>{
        window.localStorage.removeItem('auth_token');
        window.localStorage.removeItem('userInfo');
        this.router.navigate(['/signin']);
      })
      .catch(err=>{
        alert('退出失败,请稍后重试');
      })
  }

}
